/*
 * @lc app=leetcode.cn id=374 lang=cpp
 *
 * [374] 猜数字大小
 */

// @lc code=start
/** 
 * Forward declaration of guess API.
 * @param  num   your guess
 * @return 	     -1 if num is lower than the guess number
 *			      1 if num is higher than the guess number
 *               otherwise return 0
 * int guess(int num);
 */

#include <limits.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

int guess(int num);

class Solution {
public:
    int guessNumber(int n) {
        long long int left = 1;       // 正确值所在的左边界
        long long int right = n;      // 正确值所在的右边界

        while (left != right) {
            long long int mid = (left + right) / 2;
            int res = guess(mid);
            if (res == -1) {
                right = mid - 1;
            } else if (res == 1) {
                left = mid + 1;
            } else {
                left = mid;
                right = mid;
            }
        }
        
        return left;
    }
};
// @lc code=end

